package com.roger.tool.utils;

import android.content.Context;
import android.telephony.TelephonyManager;

/**
 * @author lch
 * @类说明: 运营商 simCard相关
 */
public class OperatorUtils {
	/**
	 * 移动运营商sim卡
	 */
	public static final String OPERATOR_MOBILE = "chinamobile";
	/**
	 * 联通运营商sim卡
	 */
	public static final String OPERATOR_UNICOM = "chinaunicom";
	/**
	 * 电信运营商sim卡
	 */
	public static final String OPERATOR_TELECOM = "chinatele";

	/**
	 * sim卡是否可读
	 * 
	 * @param pContext
	 * @return
	 */
	public static boolean isSimCardReady(Context pContext) {
		try {
			TelephonyManager telMgr = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);
			return TelephonyManager.SIM_STATE_READY == telMgr.getSimState();
		} catch (Exception e) {

		}
		return false;
	}

	/**
	 * 获取运营商 手机卡类型：移动、联通、电信
	 * 
	 * @return 运营商 :OPERATOR_MOBILE/OPERATOR_UNICOM/OPERATOR_TELECOM/unknown
	 */
	public static String getOperator(Context context) {

		try {
			TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			String imsi = tm.getSubscriberId();
			if (imsi == null) {
				return "unknown";
			} else if (imsi.startsWith("46000") || imsi.startsWith("46002") || imsi.startsWith("46007")
					|| imsi.startsWith("46008") || imsi.startsWith("46010")) {
				// 移动
				return OPERATOR_MOBILE;
			} else if (imsi.startsWith("46001") || imsi.startsWith("46006") || imsi.startsWith("46009")) {
				// 联通
				return OPERATOR_UNICOM;
			} else if (imsi.startsWith("46003") || imsi.startsWith("46005") || imsi.startsWith("46011")) {
				// 电信
				return OPERATOR_TELECOM;
			}
		} catch (Exception e) {

		}

		return "unknown";
	}

}
